import os

import cv2
import numpy as np
from operate import confirm_step, left_click, pic_click
import time
from variable import Variable


def conform_position(where):
    position = {
        "dorm": {
            "src": "./images/dorm/zhiqin.png",
            "w": 140,
            "h": 145,
            "bl": 1300,
            "bt": 830,
        },
        "fight_roles": {
            "src": "./images/fight/fight_roles/fight_roles.png",
            "w": 260,
            "h": 95,
            "bl": 60,
            "bt": 490,
        },
        "fight_one": {
            "src": "./images/fight/fight_one/fight_one_store.png",
            "w": 120,
            "h": 110,
            "bl": 1000,
            "bt": 825,
        },
        "fight_one_map": {
            "src": "./images/fight/fight_one/position_fight_one_map.png",
            "w": 240,
            "h": 85,
            "bl": 1415,
            "bt": 230,
        },
        "fight_two": {
            "src": "./images/fight/fight_two/position_fight_two.png",
            "w": 300,
            "h": 100,
            "bl": 30,
            "bt": 180,
        },
        "fight_three": {
            "src": "./images/fight/fight_three/position_fight_three.png",
            "w": 240,
            "h": 95,
            "bl": 40,
            "bt": 855,
        },
    }

    return confirm_step(
        position.get(where)["src"],
        w=position.get(where)["w"],
        h=position.get(where)["h"],
        bl=position.get(where)["bl"],
        bt=position.get(where)["bt"],
    )


def go_position(where):
    position = {
        "dorm": {"src": "./images/dorm.png", "delay": 3},
        "fight": {"src": "./images/fight/fight.png", "delay": 3},
    }
    flag = conform_position(where)
    if not flag:
        where0 = where
        if where in [
            "fight_roles",
            "fight_one",
            "fight_one_map",
            "fight_two",
            "fight_three",
        ]:
            where0 = "fight"
        go_home()
        pic_click(
            src=position[where0]["src"],
            delay=position[where0]["delay"],
        )
        if where == "fight_roles":
            left_click(Variable.width / 1.4, Variable.height - 25 / 2 * Variable.scale)
            left_click(Variable.width / 13.5, Variable.height / 1.4)
        elif where == "fight_one":
            left_click(Variable.width / 9.5, Variable.height / 2.5)
        elif where == "fight_one_map":
            left_click(Variable.width / 9.5, Variable.height / 2.5)
            left_click(Variable.width / 1.1786, Variable.height / 1.1357)
        elif where == "fight_two":
            left_click(Variable.width / 4.2, Variable.height / 2.5)
        elif where == "fight_three":
            left_click(Variable.width / 1.12, Variable.height - 25 / 2 * Variable.scale)
            left_click(Variable.width / 10.8875, Variable.height / 1.2785)
            left_click(Variable.width / 2.676, Variable.height / 1.95)


def go_home():
    pic_click(src="./images/go_home.png")
    while not confirm_step("./images/id.png"):
        go_back()


def go_back(delay=1):
    xy = [71 / Variable.scale, 109.5 / Variable.scale]
    left_click(xy[0], xy[1], delay=0)
    time.sleep(delay)


def conform_tili():
    if confirm_step("./images/tili/addtili.png", w=350, h=250, bl=1050, bt=380):
        if Variable.addTili:
            print("添加血清")
            left_click(Variable.width / 1.45, Variable.height / 1.2)
            go_back()
        left_click(Variable.width / 1.1, Variable.height / 6.3)
        return True
    return False


def changeSizeImages(source_path="./images"):
    for dir_or_file in os.listdir(source_path):
        file_path = os.path.join(source_path, dir_or_file)
        if os.path.isfile(file_path):  # 判断是否为文件
            if file_path == "./images\icon.png" or "current" in file_path:
                continue
            img = cv2.imdecode(np.fromfile(file_path, dtype=np.uint8), -1)
            res = cv2.resize(
                img,
                None,
                fx=Variable.scale / 2,
                fy=Variable.scale / 2,
            )
            cv2.imencode(".png", res)[1].tofile(file_path)
        elif os.path.isdir(file_path):  # 如果是个dir，则再次调用此函数，传入当前目录，递归处理。
            changeSizeImages(file_path)


if __name__ == "__main__":
    print(confirm_step("./images/back.png"))
